﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Threading;

namespace PresentItNow
{
    /// <summary>
    /// Interaction logic for Viewer.xaml
    /// </summary>
    public partial class Viewer : Window
    {
        public Viewer(IContract channel, string name)
        {
            InitializeComponent();

            this.channel = channel;

            Loaded += new RoutedEventHandler(Viewer_Loaded);

            clientInfo.Name = name;
        }

        void Viewer_Loaded(object sender, RoutedEventArgs e)
        {
            Thread continuousReadThread = new Thread(new ThreadStart(LoadScreen));
            continuousReadThread.IsBackground = true;
            continuousReadThread.Start();
        }

        ClientInfo clientInfo = new ClientInfo();

        private void LoadScreen()
        {
            while (!stopped)
            {
                double mouseX = mousePosition.X;
                double mouseY = mousePosition.Y;

                clientInfo.PointToX = pictureWidth / imageWidth * mouseX;
                clientInfo.PointToY = pictureHeight / imageHeight * mouseY;

                byte[] pictureBuffer = null;

                try
                {                    
                    pictureBuffer = channel.GetPicture(clientInfo, ImageQuality);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lost connection");                                        
                    break;
                }

                if (isReady)
                    Dispatcher.BeginInvoke((Action<byte[]>)RenderImage, pictureBuffer);
            }
            Dispatcher.BeginInvoke((Action)this.Close);
            
        }

        bool stopped = false;

        bool isReady = true;

        private void RenderImage(byte[] pictureBuffer)
        {
            isReady = false;
            Picture = LoadImage(pictureBuffer);
            pictureWidth = Picture.Width;
            pictureHeight = Picture.Height;
            image.Source = Picture;
            
            isReady = true;

            imageWidth = image.ActualWidth;
            imageHeight = image.ActualHeight;
        }

        double pictureWidth, pictureHeight;

        double imageWidth, imageHeight;


        IContract channel = null;

        public BitmapSource Picture { get; set; }

        BitmapSource LoadImage(Byte[] imageData)
        {
            using (MemoryStream ms = new MemoryStream(imageData))
            {
                var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                return decoder.Frames[0];
            }
        }

        private void image_MouseMove(object sender, MouseEventArgs e)
        {
            mousePosition = Mouse.GetPosition(image);
        }

        Point mousePosition;

        public double ImageQuality { get; set; }
        
    }
}
